package org.ssi.web.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.filter.OncePerRequestFilter;

/**
 * @author 
 */
public class PageAccessFilter extends OncePerRequestFilter {
	private final Log log = LogFactory.getLog(getClass());

	private String ignorePages;

	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
			throws ServletException, IOException {
		String requestPage = StringUtils.substringAfterLast(request.getRequestURI(), "/");
		if (StringUtils.isNotBlank(requestPage)
				&& !StringUtils.contains(("," + ignorePages + ","), ("," + requestPage + ","))) {
			if (log.isDebugEnabled()) {
				log.debug(ignorePages + " will be ignored");
			}
			response.sendError(403);
			return;
		}
		chain.doFilter(request, response);
	}

	public String getIgnorePages() {
		return ignorePages;
	}

	public void setIgnorePages(String ignorePages) {
		this.ignorePages = ignorePages;
	}

}
